package com.ctg.behavior.client.impala.utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CtCalcSQLParserUtils {

	public static String removeOrderByCalcSQL(String sql) {
		// tab、回车等特殊字符
		Pattern p = Pattern.compile("\t|\r|\n");
		Matcher m = p.matcher(sql);
		sql = m.replaceAll(" ");
		// 移除多余的连续空格，只留下一个空格
		p = Pattern.compile("\\s{2,}");
		m = p.matcher(sql);
		sql = m.replaceAll(" ");

		return sql;
	}

	public static List<String> fetchSelectFields(String sql) {
		List<String> fields = new ArrayList<>();
		// 1.格式化SQL
		sql = CtCalcSQLParserUtils.formatCalcSQL(sql);
		// 2.提交顶层SELECT部分
		int endIndex = sql.indexOf("FROM");
		if (endIndex < 0) {
			endIndex = sql.indexOf("from");
		}
		if (endIndex <= 0) {
			return fields;
		}
		String selectPartSQL = sql.substring(0, endIndex);
		// selectPartSQL = selectPartSQL.toUpperCase();
		int index = 0;
		do {
			index = selectPartSQL.indexOf(" AS ", index);
			if (index < 0) {
				index = selectPartSQL.indexOf(" as ", index);
			}
			if (index < 0) {
				break;
			}
			index += 4;
			endIndex = selectPartSQL.indexOf(",", index);
			if (endIndex < 0) {
				endIndex = selectPartSQL.indexOf(" ", index);
			}
			String oneField = selectPartSQL.substring(index, endIndex);
			fields.add(oneField.trim());
		} while (true);

		return fields;
	}

	public static String formatCalcSQL(String sql) {
		// tab、回车等特殊字符
		Pattern p = Pattern.compile("\t|\r|\n");
		Matcher m = p.matcher(sql);
		sql = m.replaceAll(" ");
		// 移除多余的连续空格，只留下一个空格
		p = Pattern.compile("\\s{2,}");
		m = p.matcher(sql);
		sql = m.replaceAll(" ");

		return sql;
	}

	public static String fetchGroupBy(String sql) {
		int index = sql.lastIndexOf(')');
		if (index <= 0) {
			return "";
		}
		index = sql.indexOf("GROUP BY", index);
		if (index <= 0) {
			return "";
		}
		int endpos = sql.indexOf("LIMIT", index);
		if (endpos < 0) {
			endpos = sql.indexOf("ORDER", index);
		}
		if (endpos > 0) {
			return sql.substring(index, endpos);
		} else {
			return sql.substring(index);
		}
	}

//	public static String formatKeys2UpperCase(String sql) {
//		String[] sqlFields = sql.split(" ");
//		StringBuilder sbuf = new StringBuilder(sql);
//		for (int index = 0; index < sqlFields.length; index++) {
//			String upperFiled = sqlFields[index].toUpperCase();
//			if (sqlKeys.contains(upperFiled) && !sqlKeys.contains(sqlFields[index])) {
//				int spos = sbuf.indexOf(sqlFields[index]);
//				sbuf.replace(spos, spos + sqlFields[index].length(), upperFiled);
//			}
//		}
//		return sbuf.toString();
//	}

	private static final Set<String> sqlKeys = new HashSet<String>();

	static {
		sqlKeys.add("LEFT");
		sqlKeys.add("RIGHT");
		sqlKeys.add("INNER");
		sqlKeys.add("OUTER");
		sqlKeys.add("GROUP");
		sqlKeys.add("ORDER");

		sqlKeys.add("WHERE");
		sqlKeys.add("SET");
		sqlKeys.add("HAVING");
		sqlKeys.add("JOIN");
		sqlKeys.add("FROM");
		sqlKeys.add("BY");
		sqlKeys.add("JOIN");
		sqlKeys.add("INTO");
		sqlKeys.add("UNION");

		sqlKeys.add("AND");
		sqlKeys.add("OR");
		sqlKeys.add("WHEN");
		sqlKeys.add("ELSE");
		sqlKeys.add("END");

		sqlKeys.add("IN");
		sqlKeys.add("ALL");
		sqlKeys.add("EXISTS");
		sqlKeys.add("SOME");
		sqlKeys.add("ANY");

		sqlKeys.add("INSERT");
		sqlKeys.add("UPDATE");
		sqlKeys.add("DELETE");

		sqlKeys.add("SELECT");
		sqlKeys.add("ON");
	}

}
